<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>父页面</title>
</head>
<body>
    <h1>父页面</h1>

    <!-- 嵌入iFrame -->
    <iframe id="myIframe" src="iframe.html" width="400" height="300"></iframe>

    <button id="sendMessageButton">发送消息到 iFrame</button>

    <script>
		
        // 获取iframe元素
        const iframe = document.getElementById('myIframe');

        // 监听来自iFrame的消息
        window.addEventListener('message', function(event) {
            // 验证消息来源
            if (event.origin !== window.location.origin) {
                console.log('不可信的消息来源');
                return;}
            console.log('父页面接收到的消息:', event.data);
        });
 
        // 按钮点击发送消息到iFrame
        document.getElementById('sendMessageButton').addEventListener('click', function() {
            // 向iFrame发送消息
            iframe.contentWindow.postMessage('你好,iFrame!', '*');
        });
    </script>
</body>
</html>

</body>
</html>